package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.home.entity.AiteListBean;
import com.rongshu.view.home.entity.CommentDetails;
import com.rongshu.view.home.listener.OnExpandFunctionListener;
import com.rongshu.view.home.widget.CommentExpandableLinearLayout;
import com.rongshu.view.home.widget.ExpandableTextView;
import com.rongshu.view.penpal.view.PenpalCommentOnItemClickListener;
import com.widget.CircleImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

public class PostCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;
    private List<CommentDetails.ResponseBean> responseBeanList;
    //interface
    private PenpalCommentOnItemClickListener mOnItemFunctionClickListener = null;
    //payloads
    public final int THIRDCOMMENT = 0x01;
    public final int GIVELIKE = 0x02;

    public PostCommentAdapter(Context context, List<CommentDetails.ResponseBean> list) {
        this.mContext = context;
        this.responseBeanList = list;
    }

    public void setResponseBeanList(List<CommentDetails.ResponseBean> list) {
        this.responseBeanList = list;
    }

    public void addResponseBeanList(List<CommentDetails.ResponseBean> list) {
        this.responseBeanList.addAll(list);
    }

    public List<CommentDetails.ResponseBean> getResponseBeanList() {
        return responseBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_comment_item_view, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CommentDetails.ResponseBean responseBean = responseBeanList.get(position);
        CommentViewHolder viewHolder = (CommentViewHolder) holder;
        //第一个评论者头像
        GlideUtils.loadImageView(mContext, viewHolder.iv_head_icon, responseBean.getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
        //vip
        viewHolder.iv_vip.setVisibility(responseBean.isVip() ? View.VISIBLE : View.GONE);
        //bi_name
        viewHolder.txt_article_title.setText(responseBean.getBi_name());
        //data
        viewHolder.txt_article_data.setText(DateUtils.ConvertDate(responseBean.getCreate_datetime()));
        //like count
        viewHolder.txt_like_count.setVisibility(responseBean.getLikeCount() == 0 ? View.GONE : View.VISIBLE);
        viewHolder.txt_like_count.setText(responseBean.getLikeCount() == 0 ? "" : responseBean.getLikeCount() + "");
        viewHolder.txt_like_count.setTextColor(responseBean.getIslike() == 0 || responseBean.getIslike() == -1 ?
                mContext.getResources().getColor(R.color.color_9f9f9f) :
                mContext.getResources().getColor(R.color.color_f99704));
        //like
        viewHolder.iv_like.setImageResource(responseBean.getIslike() == 0 || responseBean.getIslike() == -1
                ? R.drawable.give_like_white_icon : R.drawable.give_like_yellow_icon);
        //content
        String content = null;
        try {
            String replaceContent = responseBean.getContents().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            content = URLDecoder.decode(replaceContent, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(content)) {
            //atlist
            final List<AiteListBean> aiteListBean = responseBean.getAiteList();
            viewHolder.txt_article_content.setAiteListBeanList(aiteListBean);
            viewHolder.txt_article_content.setText(content);
            viewHolder.txt_article_content.setOnExpandFunctionListener(new OnExpandFunctionListener() {
                @Override
                public void onClick() {
                    if (mOnItemFunctionClickListener != null) {
                        mOnItemFunctionClickListener.onItemClickListener(position);
                    }
                }

                @Override
                public void onAtClick(int user_id) {
                    if (mOnItemFunctionClickListener != null) {
                        mOnItemFunctionClickListener.onItemHeaderClickListener(user_id);
                    }
                }
            });
        }
        int count = responseBean.getComment_replyCount();
        if (count > 0) {
            //reply
            viewHolder.commentExpandableLinearLayout.setIsExpand(false);
            viewHolder.commentExpandableLinearLayout.setCommentReplysBeanList(responseBean.getComment_replys(), count);
            viewHolder.commentExpandableLinearLayout.setVisibility(View.VISIBLE);
        } else {
            viewHolder.commentExpandableLinearLayout.setVisibility(View.GONE);
        }

        //设置展开字体
        if (count > 1) {
            viewHolder.commentExpandableLinearLayout.setExpandText("展开" + (count - 1) + "条回复");
        }
        //展开点击
        viewHolder.commentExpandableLinearLayout.setOnStateChangeListener(new CommentExpandableLinearLayout.OnStateChangeListener() {
            @Override
            public void onStateChanged(boolean isExpanded) {
                if (mOnItemFunctionClickListener != null) {
                    mOnItemFunctionClickListener.onItemExpandClickListener(isExpanded,
                            responseBean.getComment_replyCount(), responseBean.getId(), position);
                }
            }
        });
        //二级评论点击
        viewHolder.commentExpandableLinearLayout.setOnItemClickListener(new CommentExpandableLinearLayout.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (mOnItemFunctionClickListener != null && responseBean.getComment_replys() != null) {
                    if (responseBean.getComment_replys().size() < position + 1) {
                        return;
                    }
                    //评论commentid
                    int comment_id = responseBean.getId();
                    //评论评论reply_id
                    int reply_id = responseBean.getComment_replys().get(position).getId();
                    //对谁评论的评论者姓名
                    String bi_name = responseBean.getComment_replys().get(position).getFrom_Name();
                    //对谁评论的评论者id
                    int reply_user_id = responseBean.getComment_replys().get(position).getFrom_uid();
                    if (mOnItemFunctionClickListener != null) {
                        mOnItemFunctionClickListener.onItemChildClickListener(bi_name, comment_id, reply_id, reply_user_id);
                    }
                }
            }

            //二级评论@和头像点击
            @Override
            public void onItemAtAndHeaderClick(int user_id,boolean is_allow_browse_homepage) {
                if (mOnItemFunctionClickListener != null) {
                    mOnItemFunctionClickListener.onItemHeaderClickListener(user_id);
                }
            }
        });

        //头像点击
        viewHolder.iv_head_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemFunctionClickListener != null) {
                    mOnItemFunctionClickListener.onItemHeaderClickListener(responseBean.getFrom_uid());
                }
            }
        });
        //点赞点击
        viewHolder.layout_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemFunctionClickListener != null) {
                    mOnItemFunctionClickListener.onItemGiveLikeClickListener(responseBean.getId(), position);
                }
            }
        });
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            //viewholder
            CommentViewHolder viewHolder = (CommentViewHolder) holder;
            //CommentDetails
            final CommentDetails.ResponseBean responseBean = responseBeanList.get(position);
            //获取局部刷新标识
            int payload = (int) payloads.get(0);
            switch (payload) {
                case THIRDCOMMENT://二级评论
                    viewHolder.commentExpandableLinearLayout.setIsExpand(true);
                    viewHolder.commentExpandableLinearLayout.setCommentReplysBeanList(responseBean.getComment_replys(), responseBean.getComment_replys().size());
                    break;
                case GIVELIKE://点赞
                    //like count
                    viewHolder.txt_like_count.setVisibility(responseBean.getLikeCount() == 0 ? View.GONE : View.VISIBLE);
                    viewHolder.txt_like_count.setText(responseBean.getLikeCount() == 0 ? "" : responseBean.getLikeCount() + "");
                    viewHolder.txt_like_count.setTextColor(responseBean.getIslike() == 0 || responseBean.getIslike() == -1 ?
                            mContext.getResources().getColor(R.color.color_9f9f9f) :
                            mContext.getResources().getColor(R.color.color_f99704));
                    //like
                    viewHolder.iv_like.setImageResource(responseBean.getIslike() == 0 || responseBean.getIslike() == -1
                            ? R.drawable.give_like_white_icon : R.drawable.give_like_yellow_icon);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (responseBeanList != null) {
            return responseBeanList.size();
        }
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private static class CommentViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView iv_head_icon;
        private ImageView iv_vip, iv_like;
        private TextView txt_article_title, txt_article_data, txt_like_count;
        private LinearLayout layout_like;
        private ExpandableTextView txt_article_content;
        private CommentExpandableLinearLayout commentExpandableLinearLayout;

        public CommentViewHolder(View itemView) {
            super(itemView);
            iv_head_icon = itemView.findViewById(R.id.iv_head_icon);
            iv_vip = itemView.findViewById(R.id.iv_vip);
            iv_like = itemView.findViewById(R.id.iv_like);
            txt_article_title = itemView.findViewById(R.id.txt_article_title);
            txt_article_data = itemView.findViewById(R.id.txt_article_data);
            txt_like_count = itemView.findViewById(R.id.txt_like_count);
            layout_like = itemView.findViewById(R.id.layout_like);
            txt_article_content = itemView.findViewById(R.id.txt_article_content);
            commentExpandableLinearLayout = itemView.findViewById(R.id.commentLiearLayout);
        }
    }

    /**
     * set listener
     */
    public void setOnItemFunctionClickListener(PenpalCommentOnItemClickListener listener) {
        this.mOnItemFunctionClickListener = listener;
    }
}
